﻿using System;
using System.Collections.Generic;
using System.Linq;
using Best.SmartCms.Common;
using Best.SmartCms.Dao.Common;
using Best.SmartCms.Domain;
using Best.SmartCms.Dto.Request;
using Best.SmartCms.Dto;

namespace Best.SmartCms.Dao
{
    /// <summary>
    /// ChannelGroup数据库操作类
    /// </summary>
    public class ChannelGroupDao : BaseDao<ChannelGroup>, ICommonDao
    {
        private readonly MySqlDbContext _db;

        public ChannelGroupDao(MySqlDbContext dbContext) : base(dbContext)
        {
            _db = dbContext;
        }


        public ChannelGroup FindById(long id)
        {
            return _db.ChannelGroupSet.FirstOrDefault(p => p.Id == id);
        }

        public List<ChannelGroup> PageList(ChannelGroupListQueryDto query, out int totalCount, out int totalPages)
        {
            var result = _db.ChannelGroupSet.AsQueryable();
            if (!string.IsNullOrEmpty(query.Keywords))
            {
                result = result.Where(d => d.Name.Contains(query.Keywords));
            }

            // 排序
            if (string.IsNullOrEmpty(query.SortField))
            {
                result = result.OrderByDescending(p => p.CreatedTime);
            }
            else
            {
                
            }

            return result.PageBy(query.PageIndex, query.PageSize, out totalPages, out totalCount)
                .ToList();

        }
    }
}
